// Partially from:
// http://www.codetoad.com/javascript/form_validation_function.asp
// http://stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric
// http://www.white-hat-web-design.co.uk/blog/javascript-validation/

 

function validateRegForm()
{
    var currentField = document.forms["registerForm"]["userType"];
    // Check the user type field
    if ( IsEmpty(currentField) )
    {
        alert("User type must be selected");
        GetAttention(currentField);       
        return false;
    }
    else LoseAttention(currentField);
    
    currentField = document.forms["registerForm"]["name"];   
    // Check the name field
    if ( IsEmpty(currentField) || currentField.length < 4 )
    {
        alert("Name must be filled out properly (minimum length of 4 characters).");
        GetAttention(currentField);        
        return false;
    }
    else LoseAttention(currentField);
    
    // Check the email address field
    currentField = document.forms["registerForm"]["email"];
    if ( !IsValidEmail(currentField) ) {
        alert("Email address must be a proper email address");
        GetAttention(currentField); 
        return false;
    }
    else LoseAttention(currentField);

// Check the password field
    currentField = document.forms["registerForm"]["password"];   
    if ( IsEmpty(currentField) )
    {
        alert("Password must be filled out");
        GetAttention(currentField);         
        return false;
    }
    else LoseAttention(currentField);    
    
    // Check the password confirmation field
    currentField = document.forms["registerForm"]["passwordConf"];   
    if ( IsEmpty(currentField) )
    {
        alert("Password must be confirmed");
        GetAttention(currentField);         
        return false;
    }
    else LoseAttention(currentField);

    // Confirm passwords match
    var prevField = document.forms["registerForm"]["password"];
    if( prevField.value != currentField.value )
    {
        alert("Your password and confirmation password do not match");
        GetAttention(currentField); 
        GetAttention(prevField);  
        return false;
    }
    else {LoseAttention(currentField);LoseAttention(prevField);}
    
    // Check the mother's maiden name field
    currentField = document.forms["registerForm"]["mothersMaidenName"];   
    if ( IsEmpty(currentField) )
    {
        alert("Mother's maiden name must be filled out");
        GetAttention(currentField);        
        return false;
    }
    else LoseAttention(currentField);
    
    // If a registered user is being created, validate those fields
    var regDetails = document.getElementById("regUser");
    if (regDetails.style.display == "block" ){
        return validateRegisteredForm();
    }
    
    // If a business user is being created, validate those fields
    var businessDetails = document.getElementById("businessUser");
    if( businessDetails.style.display == "block" ) {
        return validateBusinessForm("registerForm");
    }

    return true;
    
}

function validateRegisteredForm()
{
    // Check the address field
    currentField = document.forms["registerForm"]["address"];   
    if ( IsEmpty(currentField) )
    {
        alert("Address must be filled out");
        GetAttention(currentField);         
        return false;
    }
    else LoseAttention(currentField);

    // Check the landline phone number field
    currentField = document.forms["registerForm"]["phoneNumLand"];   
    if ( !IsNumber(currentField.value) )
    {
        alert("Landline phone number must be filled out with only numbers");
        GetAttention(currentField);        
        return false;        
    }
    else LoseAttention(currentField);

    // Check the cell phone number field
    currentField = document.forms["registerForm"]["phoneNumCell"];   
    if ( !IsNumber(currentField.value) )
    {
        alert("Mobile phone number must be filled out with only numbers");
        GetAttention(currentField);       
        return false;        
    }
    else LoseAttention(currentField);
    
    // Check the occupation field
    currentField = document.forms["registerForm"]["occupation"];   
    if ( IsEmpty(currentField) )
    {
        alert("Occupation must be filled out");
        GetAttention(currentField);        
        return false;
    }
    else LoseAttention(currentField);
    
    // Check the credit card number field
    currentField = document.forms["registerForm"]["ccNumber"];   
    if ( !IsNumber(currentField.value) )
    {
        alert("Credit card number must be filled out with only numbers");
        GetAttention(currentField);         
        return false;        
    }
    else LoseAttention(currentField);

    // Check the credit card code field
    currentField = document.forms["registerForm"]["ccCode"];   
    if ( !IsNumber(currentField.value) )
    {
        alert("Credit card code must be filled out with only numbers");
        GetAttention(currentField);        
        return false;        
    }
    else LoseAttention(currentField);
    
    return true;
}

function validateBusinessForm(formName)
{
    var currentField = document.forms[formName]["companyName"];
    
    // Check the company name field
    if ( IsEmpty(currentField) )
    {
        alert("Company name must be filled out.");
        GetAttention(currentField);       
        return false;
    }
    else LoseAttention(currentField);
    
    // Check the business charter field
    currentField = document.forms[formName]["businessCharter"];
    if ( IsEmpty(currentField) )
    {
        alert("Business charter must be filled out.");
        GetAttention(currentField);       
        return false;
    }
    else LoseAttention(currentField);

    // Check the contact name field
    currentField = document.forms[formName]["contactName"];
    if ( IsEmpty(currentField) )
    {
        alert("Contact name must be filled out.");
        GetAttention(currentField);       
        return false;
    }
    else LoseAttention(currentField);
    
    
    // Check the contact phone (land) field
    currentField = document.forms[formName]["contactPhoneLand"];
    if ( !IsNumber(currentField.value) )
    {
        alert("Contact phone number (land line) must be filled out.");
        GetAttention(currentField);       
        return false;
    }
    else LoseAttention(currentField);    

    // Check the contact phone (mobile) field
    currentField = document.forms[formName]["contactPhoneMobile"];
    if ( !IsEmpty(currentField) && !IsNumber(currentField.value) )
    {
        alert("Contact phone number (mobile) must be filled out properly.");
        GetAttention(currentField);
        return false;
    }
    else LoseAttention(currentField);
    
    // Check the contact phone (fax) field
    currentField = document.forms[formName]["contactPhoneFax"];
    if ( !IsEmpty(currentField) && !IsNumber(currentField) )
    {
        alert("Contact phone number (fax) must be filled out properly.");
        GetAttention(currentField);
        return false;
    }
    else LoseAttention(currentField);
    
    // Check the contact email field
    currentField = document.forms[formName]["contactEmail"];
    if ( !IsValidEmail(currentField) )
    {
        alert("Contact email must be filled out properly.");
        GetAttention(currentField);       
        return false;
    }
    else LoseAttention(currentField);
    
    
    // Check the company city field
    currentField = document.forms[formName]["companyCity"];
    if ( IsEmpty(currentField) )
    {
        alert("Company city must be filled out.");
        GetAttention(currentField);       
        return false;
    }
    else LoseAttention(currentField);    

    // Check the company country field
    currentField = document.forms[formName]["companyCountry"];
    if ( IsEmpty(currentField) )
    {
        alert("Company country must be filled out.");
        GetAttention(currentField);       
        return false;
    }
    else LoseAttention(currentField);  

    // Check the preferred industry field
    currentField = document.forms[formName]["preferredIndustry"];
    if ( IsEmpty(currentField) )
    {
        alert("Preferred industry must be filled out.");
        GetAttention(currentField);       
        return false;
    }
    else LoseAttention(currentField);
    
    return true;
    
}


function validateUpdateForm() {
    
    var currentField = document.forms["updateForm"]["email"];
    
    // Check the email address field
    if ( !IsValidEmail(currentField) ) {
        alert("Email address must be a proper email address");
        GetAttention(currentField); 
        return false;
    }
    else LoseAttention(currentField);
    
    if (document.getElementById("regUser") != null)
        return validateUpdateRegisteredForm();
    else if (document.getElementById("businessUser") != null)
        return validateBusinessForm("updateForm");

    return false;    
    
}

function validateUpdateRegisteredForm()
{
    currentField = document.forms["updateForm"]["name"];   
    // Check the name field
    if ( IsEmpty(currentField) || currentField.length < 4 )
    {
        alert("Name must be filled out properly (minimum length of 4 characters).");
        GetAttention(currentField);        
        return false;
    }
    else LoseAttention(currentField);
    
    // Check the address field
    currentField = document.forms["updateForm"]["address"];   
    if ( IsEmpty(currentField) )
    {
        alert("Address must be filled out");
        GetAttention(currentField);         
        return false;
    }
    else LoseAttention(currentField);
    
    // Check the landline phone number field
    currentField = document.forms["updateForm"]["phoneNumLand"];   
    if ( !IsNumber(currentField.value) )
    {
        alert("Landline phone number must be filled out with only numbers");
        GetAttention(currentField);        
        return false;        
    }
    else LoseAttention(currentField);

    // Check the cell phone number field
    currentField = document.forms["updateForm"]["phoneNumCell"];   
    if ( !IsNumber(currentField.value) )
    {
        alert("Mobile phone number must be filled out with only numbers");
        GetAttention(currentField);       
        return false;        
    }
    else LoseAttention(currentField);
    
    // Check the occupation field
    currentField = document.forms["updateForm"]["occupation"];   
    if ( IsEmpty(currentField) )
    {
        alert("Occupation must be filled out");
        GetAttention(currentField);        
        return false;
    }
    else LoseAttention(currentField);
    
    // Check the blackmark
    currentField = document.forms["updateForm"]["blackmarks"];   
    if ( !IsNumber(currentField.value) )
    {
        alert("Blackmark must be a number.");
        GetAttention(currentField);        
        return false;        
    }
    else LoseAttention(currentField);
    
    // Check that the value of the blackmark field is a valid value
    if ( currentField.value < 0 || currentField.value > 3 )
    {
        alert("Blackmark must be between 0 and 3.");
        GetAttention(currentField);        
        return false;        
    }
    else LoseAttention(currentField);    
    
    return true;
}

function IsEmpty(field) {
   if( (field.value.length==0) || (field.value==null) ) 
   {
      return true;
   }
   else return false;
}

function IsNumber(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}

function IsValidEmail(field) {
 
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = field.value;
   if(reg.test(address) == false) {
      return false;
   }
   return true;
}

function GetAttention(currentField) {
    currentField.focus();
    currentField.style.backgroundColor = "#FF6F6F";  
}

function LoseAttention(currentField) {
        currentField.style.backgroundColor = "#FFFFFF";      
}

function showForm(id) {
    var formToShow = document.getElementById(id);
    formToShow.style.display = "block";
}

function hideForm(id) {
    var formToShow = document.getElementById(id);
    formToShow.style.display = "none";
}


